#= require jquery
#= require jquery_ujs
#= require turbolinks
#= require bootstrap/transition
#= require bootstrap/modal
#= require bootstrap/treenav
#= require select2
#= require select2_locale_zh-CN
#= require dataTables/jquery.dataTables
#= require dataTables/rowReorder
#= require dataTables/bootstrap/3/jquery.dataTables.bootstrap
#= require dataTables/extras/dataTables.select
#= require ckeditor/init
#= require cocoon
#= require moment
#= require moment/zh-cn
#= require bootstrap-datetimepicker
#= require pickers

$.extend true, $.fn.dataTable.defaults,
  language:
    emptyTable: "表中数据为空"
    info: "从 _START_ 到 _END_ / 共 _TOTAL_ 条数据"
    infoEmpty: "没有数据"
    infoFiltered: "（从 _MAX_ 条数据中检索）"
    lengthMenu: "每页显示 _MENU_ 条记录"
    zeroRecords: "没有检索到数据"
    search: "搜索："
    paginate:
      first: "«"
      previous: "‹"
      next: "›"
      last: "»"

$.fn.dropzone = (options) ->
  $page_avatar = $(this)
  $page_avatar_thumb = $(options.preview)

  $page_avatar_thumb.on "click", () ->
      event.preventDefault()
      event.stopPropagation()
      $page_avatar.trigger("click")
    .on "dragover", (event) ->
      event.preventDefault()
      event.stopPropagation()
      $page_avatar_thumb.addClass('dz-drag-hover')
    .on "dragleave", (event) ->
      event.preventDefault()
      event.stopPropagation()
      $page_avatar_thumb.removeClass('dz-drag-hover')
    .on "drop", (event) ->
      event.preventDefault()
      event.stopPropagation()
      $page_avatar_thumb.removeClass('dz-drag-hover')
      $page_avatar[0].files = event.originalEvent.dataTransfer.files

  $page_avatar.on "change", () ->
    URL = window.URL || window.webkitURL
    src = URL.createObjectURL(this.files[0])
    $page_avatar_thumb.find("img").attr("src", src)

$(document).on "ready turbolinks:load", ->
  $('.select2')
    .addClass('initialized')
    .select2
      theme: 'bootstrap'
      language: 'zh-CN'

  $('ul[data-toggle=nav-tree]:not(.initialized)')
    .addClass('initialized')
    .each ->
      $tree = $(this)
      $tree.navTree($tree.data())

  reorder = $('.table.datatable:not(.initialized)').data('reorder') == true
  $('.table.datatable:not(.initialized)')
    .addClass('initialized')
    .DataTable
      # order: [] # Do not sort by default
      pagingType: 'numbers'
      rowReorder: reorder

  $('.ckeditor:not(.initialized)')
    .addClass('initialized')
    .each ->
      CKEDITOR.replace $(this).attr('id'),
        language: 'zh-cn'
        font_names: '黑体/-apple-system, "Helvetica Neue", Helvetica, "Nimbus Sans L", Arial, "Liberation Sans", "PingFang SC", "Hiragino Sans GB", "Source Han Sans CN", "Source Han Sans SC", "Microsoft YaHei", "Wenquanyi Micro Hei", "WenQuanYi Zen Hei", "ST Heiti", SimHei, "WenQuanYi Zen Hei Sharp", sans-serif;楷体/Baskerville, Georgia, "Liberation Serif", "Kaiti SC", STKaiti, "AR PL UKai CN", "AR PL UKai HK", "AR PL UKai TW", "AR PL UKai TW MBE", "AR PL KaitiM GB", KaiTi, KaiTi_GB2312, DFKai-SB, "TW-Kai", serif;宋体/Georgia, "Nimbus Roman No9 L", "Songti SC", STSong, "AR PL New Sung", "AR PL SungtiL GB", NSimSun, SimSun, "TW-Sung", "WenQuanYi Bitmap Song", "AR PL UMing CN", "AR PL UMing HK", "AR PL UMing TW", "AR PL UMing TW MBE", PMingLiU, MingLiU, serif;放送/Baskerville, "Times New Roman", "Liberation Serif", STFangsong, FangSong, FangSong_GB2312, "CWTEX-F", serif;'

$(document).on 'turbolinks:request-end', ->
  $('.table.datatable.initialized')
    .removeClass('initialized')
    .DataTable().destroy()
  # tinymce.remove()
